import 'package:flutter/material.dart';
import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';
import 'package:xbaistack/model/model.dart';
import 'package:xbaistack/widget/refresh_list.dart';

class MasonryList<M extends Model<M>> extends RefreshList<M> {
  final int column;
  final double spacing;

  MasonryList({
    this.column = 2,
    this.spacing = 10,
    required super.controller,
    required super.builder,
  });

  @override
  State<StatefulWidget> createState() => _MasonryListState<M>();
}

class _MasonryListState<M extends Model<M>>
    extends RefreshListState<M, MasonryList<M>> {
  @override
  Widget widgetOf(
    BuildContext context,
    int count,
    List<M> datas,
    double cacheExtent,
  ) {
    return MasonryGridView.count(
      crossAxisCount: widget.column,
      mainAxisSpacing: widget.spacing,
      crossAxisSpacing: widget.spacing,
      padding: EdgeInsets.all(widget.spacing),
      itemCount: count,
      itemBuilder: (context, index) {
        return widget.builder.call(context, index, datas, items![index]);
      },
    );
  }
}
